import request from '@/utils/request'

// 获取订单列表
export function getOrders(params) {
  return request({
    url: '/api/orders',
    method: 'get',
    params
  })
}

// 获取订单详情
export function getOrder(id) {
  if (!id) {
    console.error('getOrder: 缺少id参数');
    return Promise.reject(new Error('缺少订单ID'));
  }
  
  console.log('调用getOrder API，参数:', { id });
  
  return request({
    url: `/api/orders/${id}`,
    method: 'get'
  }).then(response => {
    console.log('getOrder API响应:', response);
    return response;
  }).catch(error => {
    console.error('getOrder API错误:', error);
    throw error;
  });
}

// 创建订单
export function createOrder(data) {
  return request({
    url: '/api/orders',
    method: 'post',
    data
  })
}

// 更新订单
export function updateOrder(id, data) {
  return request({
    url: `/api/orders/${id}`,
    method: 'patch',
    data
  })
}

// 删除订单
export function deleteOrder(id) {
  return request({
    url: `/api/orders/${id}`,
    method: 'delete'
  })
}

// 更新订单状态
export function updateOrderStatus(id, status) {
  return request({
    url: `/api/orders/${id}/status`,
    method: 'put',
    data: { status }
  })
}

export function shipOrder(orderId, data) {
  return request({
    url: `/api/orders/${orderId}/ship`,
    method: 'post',
    data
  })
}

// 更新订单数量
export function updateOrderQuantity(orderId, quantity) {
  return request({
    url: `/api/orders/${orderId}/quantity`,
    method: 'put',
    data: { quantity }
  })
}

// 订单支付
export function payOrder(orderId) {
  return request({
    url: `/api/orders/${orderId}/pay`,
    method: 'post'
  })
}

// 更新订单发货状态
export function updateOrderShippingStatus(orderId, status) {
  return request({
    url: `/api/orders/${orderId}/shipping-status`,
    method: 'put',
    data: { status }
  })
}

// 取消订单
export function cancelOrder(orderId) {
  return request({
    url: `/api/orders/${orderId}/cancel`,
    method: 'post'
  })
}

// 完成订单
export function completeOrder(orderId) {
  return request({
    url: `/api/orders/${orderId}/complete`,
    method: 'post'
  })
}
